﻿
var userImpl = require( '../Business/UserImpl' );
var async = require( 'async' );

/*
 * GET home page.
 */

exports.index = function ( req, res ) {

    var CurrentUser = req.session.CurrentUser;

    if ( CurrentUser != null ) {

        var EmployeeNo = CurrentUser.EmployeeNo;

        //并行执行
        async.parallel( {

            GetUserInfoByEmployeeNo: function ( callback ) {

                userImpl.GetUserInfoByEmployeeNo( EmployeeNo, function ( userInfo ) {
                    callback( null, userInfo );
                });
            },

            GetMenuByUser: function ( callback ) {

                var menu = userImpl.GetMenuByUser( EmployeeNo );
                callback( null, menu );
            },

            GetSubMenuByUser: function ( callback ) {

                var subMenu = userImpl.GetSubMenuByUser( EmployeeNo );
                callback( null, subMenu );
            },

            GetMyApproval: function ( callback ) {

                var myApproval = userImpl.GetMyApproval( EmployeeNo );
                callback( null, myApproval );
            },

            GetMyApplcations: function ( callback ) {

                var myApplication = userImpl.GetMyApplcations( EmployeeNo );
                callback( null, myApplication );
            }


        }, function ( err, results ) {

                if ( err != null ) {
                    console.error( err );
                }

                var menu = results.GetMenuByUser,
                    subMenu = results.GetSubMenuByUser;

                req.session.Menu = menu;
                req.session.SubMenu = subMenu;

                res.render( 'index', {
                    title: '人事管理系统',
                    Menus: menu,
                    hasSubMenu: subMenu.length > 0,
                    SubMenus: subMenu,
                    myApproval: results.GetMyApproval,
                    myApplication: results.GetMyApplcations,
                    user: results.GetUserInfoByEmployeeNo,
                    UpdateTime: '2014-06-27'
                });

            });
    }
};

exports.comment = function( req, res){
        
};